1476D - Journey - CodeForces Solution


dfs and similar dp dsu implementation *1700

Please click on ads to support us..

Python Code:

import sys
input = sys.stdin.readline

for _ in range(int(input())):
    n = int(input())
            directions = input().strip()

    left = [i for i in range(n+1)]
    right = left.copy()

    for i in range(n-1, -1, -1):
        if directions[i:i+2] == 'RL':
            right[i] = right[i+2]
        elif directions[i] == 'R':
            right[i] = i+1

    for i in range(1, n+1):
        if directions[i-2:i] == 'RL':
            left[i] = left[i-2]
        elif directions[i-1] == 'L':
            left[i] = i-1

    ans = [1]*(n+1)
    for i in range(n+1):
        ans[i] += i-left[i] + right[i]-i

    print(*ans)

C++ Code:

#include <bits/stdc++.h>
#define int long long
// make it faster
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;

string s; // example string: LRLRLR (L means left, R means right)
char T(char c)
{
    if (c == 'L')
        return 'R';
    if (c == 'R')
        return 'L';
}

void solve()
{
    int n;
    cin >> n;
    cin >> s;
    vector<vector<int>> graph(2 * n + 2);
    for (int i = 0; i < n; i++)
    {
        if (s[i] == 'L')
        {
            graph[i * 2 + 2].push_back(i * 2 + 1);
            graph[i * 2 + 1].push_back(i * 2 + 2);
        }
        else
        {
            graph[i * 2].push_back(i * 2 + 3);
            graph[i * 2 + 3].push_back(i * 2);
        }
    }

    vector<int> C(n * 2 + 2, -1);
    vector<int> q;

    for (int i = 0; i < 2 * n + 2; i++)
    {
        if (C[i] != -1)
            continue; // already visited
        int a = 1;
        int q_size = q.size();
        queue<int> Q;
        C[i] = q_size;
        Q.push(i);

        while (!Q.empty())
        {
            int u = Q.front();
            Q.pop();
            for (int v : graph[u])
            {
                if (C[v] == -1)
                {
                    a++;
                    C[v] = q_size;
                    Q.push(v);
                }
            }
        }

        q.push_back(a);
    }

    for (int i = 0; i <= n; i++)
    {
        cout << q[C[i * 2]] << " ";
    }
    cout << endl;
    return;
}

int32_t main()
{
    // fasio
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int q;

    cin >> q;
    while (q--)
    {
        solve();
    }

    return 0;
}
  	 	  		   				  		  						  	


Comments

Submit
0 Comments
More Questions

1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading